// integral/index.js
import { creditsIconMap } from '../../config/common'
const { globalData } = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: globalData.userInfo,
        scanData:{},
        scanType:false,
        creditsList: [
            {
                ...creditsIconMap['1'],
                total: 0,
                num: 0,
                roletype:'0',
            },
            {
                ...creditsIconMap['2'],
                total: 0,
                num: 0,
                roletype:'0',
            },
            {
                ...creditsIconMap['3'],
                total: 0,
                num: 0,
                roletype:'0',
            },
            {
                ...creditsIconMap['4'],
                total: 0,
                num: 0,
                roletype:'0',
            },
            {
                ...creditsIconMap['5'],
                total: 0,
                num: 0,
                roletype:'0',
            }
        ],
        recordNum: 0,
        creditsType: null,
        scanRes: null,
        showPrizeDraw: false,
        prize: ""
    },
    onClickLeft() {
        wx.navigateBack()
    },
    onRecrodClick() {
        wx.navigateTo({
          url: '/credits/pages/exchange-record/index',
        })
    },
    onExchange(event) {
        console.log(event)
        const creditsType = this.data.creditsList[event.currentTarget.dataset.index]
        const score = event.currentTarget.dataset.score
        const type = event.currentTarget.dataset.type
        // if(type === '3' || type === '5') {
            
        // }
        wx.ajax.post('hasUserNameOrPhone.action', {}, true).then(res => {
           if(res.success) {
            this.setData({
                creditsType,score
            })
           } else {
            wx.showToast({
                title: '请先进行姓名信息和电话号码!',
              })
           }
        })
        
    },
    onCloseModal(event) {
        this.setData({
            creditsType: null,
            scanType:false
        })
        if (event.detail.refresh) {
            this.requestCreditsTotal()
        }
    },
    requestCreditsTotal() {
        wx.ajax.post('userScoreCentor.action', {}, true).then(res => {
            const result = res.result
            let creditsList = this.data.creditsList 
            creditsList.forEach(item => {
                let exist = result.datas.find(temp => temp.type == item.type)
                item.total = exist.total
                item.num = exist.num
                item.roletype=exist.roletype
            })
            this.setData({
                recordNum: result.num,
                creditsList
            })
        })
    },
    //扫码
  handleToScan() {
    wx.ajax.post('hasAuthCode.action', {}, true).then(res => {
      console.log(res)
      if (res.success) {
        wx.scanCode({
          success: (res) => {
            this.firstScan(res)
          }
        })
      } else {
        wx.showToast({
          title: res.msg,
        })
      }
    })
  },
  firstScan(res) {
    wx.ajax.post("qrCodeFind.action", {qrcode: res.result}, true).then((val) => {
      this.setData({
          scanRes: res,
          showPrizeDraw: true,
          prize: val.result
      })
    })
  },
  prizeDrawComplete() {
    this.setData({
      showPrizeDraw: false
    })
    wx.ajax.post("findQrCode.action", {qrcode: this.data.scanRes.result}, true).then((val) => {
      let effective = val.msg.indexOf('已被使用') > -1 ? false : true;
      this.setData({
          scanType: true,
          scanData: {
              effective,
              msg: val.msg,
              id: this.data.scanRes.result
          }
      })
    })
  },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.requestCreditsTotal()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    getScore() {
        wx.ajax.post("queryUserSocreCent.action", {}, true).then((res) => {
            
        });
    },
    prizeDrawClose() {
      this.setData({
        showPrizeDraw: false
      })
    }
})